package com.boge.core.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.boge.core.model.dao.mappers.UserMapper;
import com.boge.core.model.entity.User;
import com.boge.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

//	@Autowired
//	UserDao userDao;
	
	@Autowired
	UserMapper userMapper;
	
	@Override
	public int addUser(User user) {
		userMapper.addUser(user);
		return user.getUserId();
	}
	
	@Override
	public List<User> findAllUser() {
		List<User> userList = userMapper.findAllUsers();
		System.out.println("userList:" + userList);
		return userList;
	}
	
	@Override
	public User findUserByUserName(String userName) {
		System.out.println("userName:" + userName);
		User user = userMapper.findUserByUserName(userName);
//		String.valueOf(null);
		return user;
	}
	
	/**
	 * 根据条件按页查询用户
	 */
	@Override
	public List<User> getUserListByProp(int page, int rows, String userName, String userSex) {
		Map<String, String> map = new HashMap<String, String>();
		
		map.put("start", (page - 1) * rows + "");
		map.put("rows", rows + "");
		map.put("userName", userName);
		map.put("userSex", userSex);
		
		List<User> users = userMapper.getUserListByProp(map);
		
		return users;
	}
	
	/**
	 * 获取符合条件的用户总数量
	 */
	@Override
	public int getTotalCountFromUser(String userName, String userSex) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("userName", userName);
		map.put("userSex", userSex);
		return userMapper.getTotalCountFromUser(map);
	}

}
